package top.likeqc.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @see <a href="https://leetcode.cn/problems/remove-sub-folders-from-the-filesystem">1233.
 *     删除子文件夹</a>
 */
public class RemoveSubFoldersFromTheFilesystem {
    public static void main(String[] args) {
        Solution solution = new RemoveSubFoldersFromTheFilesystem().new Solution();

        List<String> strings =
                solution.removeSubfolders(
                        new String[] {
                            "/aa/ab/ac/ae",
                            "/aa/ab/af/ag",
                            "/ap/aq/ar/as",
                            "/ap/aq/ar",
                            "/ap/ax/ay/az",
                            "/ap",
                            "/ap/aq/ar/at",
                            "/aa/ab/af/ah",
                            "/aa/ai/aj/ak",
                            "/aa/ai/am/ao"
                        });
        strings.forEach(System.out::println);

        /* strings = solution.removeSubfolders(new String[]{"/a","/a/b/c","/a/b/d"});
        strings.forEach(System.out::println);

        strings = solution.removeSubfolders(new String[]{"/a/b/c","/a/b/ca","/a/b/d"});
        strings.forEach(System.out::println); */
    }
    // leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public List<String> removeSubfolders(String[] folder) {
            Arrays.sort(folder);
            List<String> ans = new ArrayList<String>();
            ans.add(folder[0]);
            for (int i = 1; i < folder.length; ++i) {
                int pre = ans.get(ans.size() - 1).length();
                if (!(pre < folder[i].length()
                        && ans.get(ans.size() - 1).equals(folder[i].substring(0, pre))
                        && folder[i].charAt(pre) == '/')) {
                    ans.add(folder[i]);
                }
            }
            return ans;
        }
    }
    // leetcode submit region end(Prohibit modification and deletion)

}
